package com.dankegongyu.lib.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;

import java.io.File;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

/**
 * https://github.com/hongyangAndroid/FitAndroid7/blob/master/app/src/main/java/com/zhy/fitandroid7/MainActivity.java
 *
 * @see com.zhihu.matisse.internal.utils.MediaStoreCompat
 */
public class FileProviderUtil {

    private FileProviderUtil() {
    }

    @Nullable
    public static Uri getUriForFile(Context context, String authority, File file) {
        Uri uri = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = getUriForFile24(context, authority, file);
            } else {
                uri = Uri.fromFile(file);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return uri;
    }

    private static Uri getUriForFile24(Context context, String authority, File file) {
        return FileProvider.getUriForFile(context, authority, file);
    }

    public static void setIntentDataAndType(Context context, Intent intent, String type, String authority, File file, boolean writeAble) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setDataAndType(getUriForFile(context, authority, file), type);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                if (writeAble) {
                    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
            } else {
                intent.setDataAndType(Uri.fromFile(file), type);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void setIntentData(Context context, Intent intent, String authority, File file, boolean writeAble) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setData(getUriForFile(context, authority, file));
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                if (writeAble) {
                    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
            } else {
                intent.setData(Uri.fromFile(file));
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("ObsoleteSdkInt")
    public static void grantPermissions(Context context, Intent intent, Uri uri, boolean writeAble) {
        try {
            int flag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
            if (writeAble) {
                flag |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            }
            intent.addFlags(flag);

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                List<ResolveInfo> resInfoList = context.getPackageManager()
                        .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    context.grantUriPermission(packageName, uri, flag);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
